######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# tableXI-oregon_pre.R
#  :: produces iv results stratifying on oregon before implementing vote by mail

rm(list=ls())
setwd('~/Dropbox/rainReplication')
        
or_pre=1

source('prelimRobust.R')  

main_iv1_fe=ivreg(vote~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted + 
	dose_prv + vote_prv + rain_day+rain_day_prev,
	subset=full,data=covs)               
	
main_iv2_fe=ivreg(vote~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted +
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev,
	subset=full,data=covs) 
    

main_iv1_fe_sum=summary(main_iv1_fe)
main_iv2_fe_sum=summary(main_iv2_fe)                                 

main_iv1_fe_sumcl=coeftest.cluster(covs[full,],main_iv1_fe,cluster1='as.factor(fe_id_num)')
main_iv2_fe_sumcl=coeftest.cluster(covs[full,],main_iv2_fe,cluster1='as.factor(fe_id_num)')

 
main_fstage1_fe=ivreg(dose
	~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted + 
	dose_prv + vote_prv + rain_day+rain_day_prev,
	subset=full,data=covs)                 
	
main_fstage2_fe=ivreg(dose
	~as.factor(fe_id_num)+
	#d_inc+
	dist_prev + 
	#midterm + pres_party + 
	#black + construction + educ + 
	#minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	#urban + retail + sos + gov + 
	comp_cq + 
	redistricted +
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev,
	subset=full,data=covs) 
    

main_fstage1_fe_sum=summary(main_fstage1_fe)
main_fstage2_fe_sum=summary(main_fstage2_fe)
                                 
main_fstage1_fe_sumcl=coeftest.cluster(covs[full,],main_fstage1_fe,cluster1='as.factor(fe_id_num)')
main_fstage2_fe_sumcl=coeftest.cluster(covs[full,],main_fstage2_fe,cluster1='as.factor(fe_id_num)') 

 
save(
main_iv1_fe,main_iv2_fe,
main_iv1_fe_sum,main_iv2_fe_sum,
main_iv1_fe_sumcl,main_iv2_fe_sumcl,  
main_fstage1_fe,main_fstage2_fe,
main_fstage1_fe_sum,main_fstage2_fe_sum,
main_fstage1_fe_sumcl,main_fstage2_fe_sumcl,
file=paste('robustness/tableXI_oregon_pre-',non.missings,'_',fes.type,'.Rdata',sep=''))    

rm(
	main_iv1_fe,main_iv2_fe,
	main_iv1_fe_sum,main_iv2_fe_sum,
	main_iv1_fe_sumcl,main_iv2_fe_sumcl,  
	main_fstage1_fe,main_fstage2_fe,
	main_fstage1_fe_sum,main_fstage2_fe_sum,
	main_fstage1_fe_sumcl,main_fstage2_fe_sumcl
)

# END      